#include "textureMaterial.h"
#include "texture.h"

#include "skybox.h"

#include <GL/glew.h>

SkyBox::SkyBox(const char* filename, const char* ext, float size){

	const char *szCubeFaces[6] = { "textures/reef_east.bmp",
								   "textures/reef_west.bmp",
								   "textures/reef_up.bmp",
								   "textures/reef_down.bmp",
								   "textures/reef_north.bmp",
								   "textures/reef_south.bmp" };

	tex = new TextureMaterial();
	tex->addTexture(Texture::loadCubeFileTexture(szCubeFaces), "");

	this->size = size;
}

SkyBox::~SkyBox(){

	delete tex;
}

void SkyBox::draw(){

	glDisable(GL_LIGHTING);
	glEnable(GL_TEXTURE_CUBE_MAP);
	tex->begin();

	glBegin(GL_QUADS);

		// Negative X
		glTexCoord3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-size, -size, size);

		glTexCoord3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-size, -size, -size);

		glTexCoord3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-size, size, -size);

		glTexCoord3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(-size, size, size);

		// positive X
		glTexCoord3f(1.0f, 1.0f, 1.0f);
		glVertex3f(size, -size, size);

		glTexCoord3f(1.0f, -1.0f, 1.0f);
		glVertex3f(size, size, size);

		glTexCoord3f(1.0f, -1.0f, -1.0f);
		glVertex3f(size, size, -size);

		glTexCoord3f(1.0f, 1.0f, -1.0f);
		glVertex3f(size, -size, -size);

		// Negative Z
		glTexCoord3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-size, size, -size);

		glTexCoord3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-size, -size, -size);

		glTexCoord3f(1.0f, 1.0f, -1.0f);
		glVertex3f(size, -size, -size);

		glTexCoord3f(1.0f, -1.0f, -1.0f);
		glVertex3f(size, size, -size);

		// Positive Z
		glTexCoord3f(1.0f, 1.0f, 1.0f);
		glVertex3f(size, -size, size);

		glTexCoord3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-size, -size, size);

		glTexCoord3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(-size, size, size);

		glTexCoord3f(1.0f, -1.0f, 1.0f);
		glVertex3f(size, size, size);

		// Negative Y
		glTexCoord3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(size, -size, size);

		glTexCoord3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(size, -size, -size);

		glTexCoord3f(1.0f, -1.0f, -1.0f);
		glVertex3f(-size, -size, -size);

		glTexCoord3f(1.0f, -1.0f, 1.0f);
		glVertex3f(-size, -size, size);

		// Positive Y
		glTexCoord3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(size, size, size);

		glTexCoord3f(1.0f, 1.0f, 1.0f);
		glVertex3f(-size, size, size);

		glTexCoord3f(1.0f, 1.0f, -1.0f);
		glVertex3f(-size, size, -size);

		glTexCoord3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(size, size, -size);

	glEnd();

	tex->end();
	glDisable(GL_TEXTURE_CUBE_MAP);
	glEnable(GL_LIGHTING);
}

